package com.lite.ui;

import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.lite.Config;
import com.lite.ConnectionConfig;
import com.lite.ConnectionInformation;

public class MulitConnectionDialog extends JInternalFrame implements
		FocusListener {

	/**
     * 
     */
	private static final long serialVersionUID = -5200934680040088833L;
	protected boolean canceled;
	public MyInternalFrame jif;
	protected JComboBox<ConnectionConfig> urlField;
	protected JTabbedPane tabbedPane;
	protected Vector<Connection> connections;

	public MulitConnectionDialog(MyInternalFrame f) {

		setTitle("Connect To A Database");
		jif = f;
		setSize(350, 250);
		buildDialogLayout();

	}

	protected void buildDialogLayout() {
		JPanel panel = new JPanel();
		tabbedPane = new JTabbedPane();
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		tabbedPane.addTab("Connec 0", new Tab(jif));
		tabbedPane.setTabComponentAt(0, new ButtonTabComponent(tabbedPane));
		panel.add(tabbedPane);
		setContentPane(panel);
	}

	public void focusGained(FocusEvent e) {
	}

	public void focusLost(FocusEvent e) {
	}

	protected JPanel getButtonPanel() {
		JPanel panel = new JPanel();
		JButton btn1 = new JButton("Add Connec");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				onCreateConnec();
			}
		});
		panel.add(btn1);
		JButton btn2 = new JButton("Ok");
		getRootPane().setDefaultButton(btn2);
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				onDialogOk();
			}
		});
		panel.add(btn2);
		JButton btn3 = new JButton("Cancel");
		btn3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				onDialogCancel();
			}
		});
		panel.add(btn3);
		return panel;
	}

	public Vector<Connection> getConnection() {
		setVisible(true);
		return connections;
	}

	protected boolean attemptConnection() {
		int count = tabbedPane.getTabCount();
		String passwd;
		String userid;
		ConnectionInformation tmpConnectionInformation;
		Connection connection;
		try {
			for (int i = 0; i < count; i++) {
				tmpConnectionInformation = new ConnectionInformation();
				ConnectionConfig tmpConnectionConfig = (ConnectionConfig) urlField
						.getSelectedItem();
				jif.setConnectionName(tmpConnectionConfig.getName());
				passwd = ((Tab) tabbedPane.getComponentAt(i)).getPasswd();
				userid = ((Tab) tabbedPane.getComponentAt(i)).getPasswd();
				tmpConnectionInformation.setUrl(tmpConnectionConfig.getUrl());
				tmpConnectionInformation.setConnectionListKey(String
						.valueOf(System.currentTimeMillis()));
				tmpConnectionInformation.setPassword(passwd);
				tmpConnectionInformation.setUserID(userid);
				tmpConnectionInformation.setNumberOfWindows(1);
				tmpConnectionInformation.setDatabaseDialect(tmpConnectionConfig
						.getDatabaseDialect());
				// setKeywords(tmpConnectionConfig.getDatabaseDialect()
				// .getKeywords());
				connection = DriverManager.getConnection(
						tmpConnectionInformation.getUrl(),
						tmpConnectionInformation.getUserID(),
						tmpConnectionInformation.getPassword());
				connection.setAutoCommit(false);
				connections.add(connection);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "Error connecting to "
					+ "database: " + e.getMessage());
		}
		return false;

	}

	protected void onDialogCancel() {
		setVisible(false);
		jif.getDesktopPane().remove(this);
	}

	protected void onDialogOk() {
		int count = tabbedPane.getTabCount();
		Connection connection;
		if (attemptConnection()) {
			setVisible(false);
			
			try {
				for(int i = 0; i < count; i++)
					connection = connections.get(i);
				statement = connection.createStatement();
			}

			catch (SQLException sqle) {
				JOptionPane.showMessageDialog(this, "Oops! " + sqle);
				System.exit(0);
			}

			// jif.setTitle(userid.toUpperCase());

			// statusBar.setUserPane(jif.getConnectionName());

			// jif.setConncetionKey(tmpConnectionInformation
			// .getConnectionListKey());
			//
			// mainFrame.getConnectionList().put(
			// tmpConnectionInformation.getConnectionListKey(),
			// tmpConnectionInformation);

			jif.setPreferredSize(new java.awt.Dimension(700, 500));
			jif.pack();
			jif.setVisible(true);
			jif.getDesktopPane().remove(this);
			jif.sqlTextArea.requestFocus();
		}
	}

	protected void onCreateConnec() {
		int count = tabbedPane.getTabCount();
		tabbedPane.addTab("New Connec", new Tab(jif));
		tabbedPane.setTabComponentAt(count, new ButtonTabComponent(tabbedPane));
		tabbedPane.setSelectedIndex(count);
	}
}

class Tab extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField useridField;
	private JPasswordField passwordField;

	public String getUserid() {
		return useridField.getText();
	}

	public String getPasswd() {
		return new String(passwordField.getPassword());
	}

	public Tab(MyInternalFrame jif) {
		setPreferredSize(new Dimension(300, 130));
		JLabel label;
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.WEST;
		gbc.insets = new Insets(2, 5, 2, 5);

		gbc.gridx = 0;
		gbc.gridy = 0;
		label = new JLabel("Userid:", JLabel.LEFT);
		add(label, gbc);

		gbc.gridy++;
		label = new JLabel("Password:", JLabel.LEFT);
		add(label, gbc);

		gbc.gridy++;
		label = new JLabel("Url:", JLabel.LEFT);
		add(label, gbc);

		gbc.gridy++;
		// label = new JLabel("Oracle:", JLabel.LEFT);

		gbc.gridx = 1;
		gbc.gridy = 0;

		useridField = new JTextField(10);
		add(useridField, gbc);

		gbc.gridy++;
		passwordField = new JPasswordField(10);
		add(passwordField, gbc);

		gbc.gridy++;
		// urlField = new JTextField("",15);

		JComboBox<ConnectionConfig> urlField = new JComboBox<ConnectionConfig>();
		List<ConnectionConfig> connectionConfigs = jif.mainFrame.getConfig()
				.getConnections();
		int size = connectionConfigs.size();
		for (int x = 0; x < size; x++) {
			urlField.addItem(connectionConfigs.get(x));
		}
		add(urlField, gbc);
	}

}
